# -*- coding: utf-8 -*-#
__author__ = 'Administrator'


from lib.ofc import openFlashChart
from lib.ofc.openFlashChart_varieties import Line

from lib.ofc.openFlashChart_varieties import (dot_value,
                                         hbar_value,
                                         bar_value,
                                         bar_3d_value,
                                         bar_glass_value,
                                         bar_sketch_value,
                                         bar_stack_value,
                                         pie_value,
                                         scatter_value,
                                         x_axis_labels,
                                         x_axis_label)

def dropItems_HighestLowest(items,low=None,high=None):
    """
    去掉销量低于或者高于一定值的宝贝
    >>> items = [{"volume":"10.0",},{"volume":"15.0",},{"volume":"8.5",},{"volume":"1.5",},]
    >>> print dropItems_HighestLowest(items,3,14)
    [{'volume': '10.0'}, {'volume': '8.5'}]
    """
    if low is not None:
        for item in items:
            if float(item["volume"]) < low:
                items.remove(item)
    if high is not None:
        for item in items:
            if float(item["volume"]) > high:
                items.remove(item)
    return items

def sortItems(items,by,desc=0):
    """
    给宝贝列表排序
    >>> items = [{"price":"10.0",},{"price":"15.0",},{"price":"8.5",},{"price":"1.5",},]
    >>> items = sortItems(items,"price",0)
    >>> print items
    [{'price': '1.5'}, {'price': '8.5'}, {'price': '10.0'}, {'price': '15.0'}]
    """
    items.sort(key=lambda x:float(x[by]),reverse=desc)
    return items

def getAvgItems(items,by):
    """计算宝贝某项的平均值
    @param items:dict,报表列表
    @param by:string,列名
    @return:float，返回平均值
    >>> items = [{"price":"10.0",},{"price":"15.0",},{"price":"8.5",},{"price":"1.5",},]
    >>> print getAvgItems(items,"price")
    8.75
    """
    n=0.0
    for item in items:
        n += float(item[by])
    return n/len(items)

def calProfit(items,basecost):
    """计算宝贝的利润
    @param items：dict,报表列表
    @param basecost:float,成本
    @return dict，增加利润值的宝贝列表
    >>> items = [{"price":"10.0",},{"price":"15.0",},{"price":"8.5",},{"price":"1.5",},]
    >>> print calProfit(items,5.0)
    [{'price': '10.0', 'cost': 5.0}, {'price': '15.0', 'cost': 10.0}, {'price': '8.5', 'cost': 3.5}, {'price': '1.5', 'cost': -3.5}]
    """
    for item in items:
        item["cost"]=float(item["price"])-basecost
    return items

def getMaxItems(items,by):
    """计算宝贝某项的属性的最大值
    @param items:dict,报表列表
    @param by:string,列名
    @return:float，返回最大值
    >>> items = [{"price":"10.0",},{"price":"15.0",},{"price":"8.5",},{"price":"1.5",},]
    >>> print getMaxItems(items,"price")
    15
    """
    max=0.0
    for item in items:
        if max < float(item[by]):
            max = float(item[by])
    return max

def getMinItems(items,by):
    """计算宝贝某项的属性的最小值
    @param items:dict,报表列表
    @param by:string,列名
    @return:float，返回最小值
    >>> items = [{"price":"10.0",},{"price":"15.0",},{"price":"8.5",},{"price":"1.5",},]
    >>> print getMaxItems(items,"price")
    1.5
    """
    min=0.0
    for item in items:
        if min > float(item[by]):
            min = float(item[by])
    return min

def sumItems(items,by):
    """计算宝贝某项属性的和
    @param items：dict,报表列表
    @param by:String,属性名
    @return float，计算的和
    >>> items = [{"price":"10.0",},{"price":"15.0",},{"price":"8.5",},{"price":"1.5",},]
    >>> print sumItems(items,"price")
    35.0
    """
    sum=0
    for item in items:
        sum +=float(item[by])
    return sum

def getChartJson(items,title,xby,yby):
    """得到用于展示报表的json串
    @param items:dict,报表列表
    @param title:String,报表显示的标题
    @param xby:String，报表x轴显示的数据
    @param yby:String,报表y轴显示的数据
    @return String，返回json串
    >>> items = [{"price":"10.0","vol":10},{"price":"15.0","vol":15},{"price":"8.5","vol":5},{"price":"1.5","vol":1},]
    >>> print getChartJson(items,"items chart test","price","vol")
    {"elements": [{"values": [10, 15, 5, 1], "type": "line"}], "x_legend": {"text": "price", "style": "{font-size: 20px; color: #778877}"}, "title": {"text": "items chart test", "style": "{color: #000000; font-size: 12px;}"}, "x_axis": {"labels": {"labels": ["10.0", "15.0", "8.5", "1.5"]}}, "y_legend": {"text": "vol", "style": "{font-size: 20px; color: #778877}"}, "y_axis": {"max": 20, "steps": 2}}
    """
    items=sortItems(items,"price")
    ofc = openFlashChart.template(title)
    x_step=len(items)/10
    xlabels=x_axis_labels(labels=[x[xby] for x in items],steps=x_step)
    ofc.set_x_axis(labels=xlabels)
    ofc.set_y_legend(yby, '{font-size: 20px; color: #778877}')
    ofc.set_x_legend(xby,'{font-size: 20px; color: #778877}')
    y_step=getMaxItems(items,yby)/10+1
    y_max=y_step*10
    ofc.set_y_axis(max=y_max,steps=y_step)
    element = Line(values=[y[yby] for y in items])
    ofc.add_element(element)
    return ofc.encode()




if __name__ == '__main__':
    import doctest
    doctest.testmod()